package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.annotation.Log;
import com.itheima.dao.EmpMapper;
import com.itheima.exception.ParameterException;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;


    @Override
    public Emp selectById(Integer id) {
        Emp emp = empMapper.findEmpById(id);
        return emp;
    }

    @Override
    @Log
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
    }

    @Override
    @Log
    public void deleteEmps(List<Integer> ids) {
        empMapper.deleteEmps(ids);
    }

    @Override
    public Integer queryEmpsStepOne(String name, Integer gender , LocalDate begin, LocalDate end) {
        return empMapper.QueryEmpsStepOne(name, gender, begin, end);
    }

    @Override
    public List<Emp> queryEmpsStepTwo(String name, Integer gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        Integer currentPage = (page-1)*pageSize;
        empMapper.QueryEmpsStepTwo(name, gender, begin, end, currentPage, pageSize);
        return null;
    }

//根据id查询
    @Override
    public Emp getEmpById(Integer id) {
        return   empMapper.getEmpById(id);
    }

    @Override
    @Log
    public void addEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.addEmp(emp);
    }

    @Override
    public void upload(File file) {

        try {
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream("E:/test/image.png");
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = fis.read(buf)) != -1) {
            fos.write(buf,0,len);

            }
            fis.close();
            fos.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
        }

    @Override
    public Emp login(Emp emp) {
        Emp login = empMapper.login(emp);
        return login;
    }


    //分页查询
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);//page是此页pageSize页面数据大小
        //执行条件分页查询total的值
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        //获取查询结果，要使用Page<T>中特有的方法
        Page<Emp> p = (Page<Emp>) empList;
        //封装PageBean
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }


    /**
     * 第二次实现分页查询
     * @param page
     * @param pageSize
     * @param name
     * @param gender
     * @param begin
     * @param end
     * @return
     */
    @Override
    public PageBean pageDemo(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
        //使用pageHelper
        PageHelper.startPage(page,pageSize);


        //调取mapper层方法
        List<Emp> emps = empMapper.listDemo(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) emps;
       return new PageBean(p.getTotal(),p.getResult());


    }

    @Override
    public void deleteByDeptId(Integer deptId) {
        empMapper.deleteEmpByDeptId(deptId);
    }

}
